package com.wang.cloud.manage.service.manage;

import com.wang.cloud.common.core.exception.ServiceException;
import com.wang.cloud.manage.domain.vo.RoleNameVo;
import com.wang.cloud.manage.service.IRoleService;
import com.wang.cloud.manage.service.IUserRoleRelationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @author wang ye
 * @date 2024/7/16
 */
@Slf4j
@Service
public class SysUserRoleManage {

    @Resource
    private IRoleService roleService;
    @Resource
    private IUserRoleRelationService roleRelationService;
    ;

    @Transactional(rollbackFor = Exception.class)
    public void updateUserRole(Long userId, Collection<Long> roleIds) {
        if (!roleService.allExists(roleIds)) {
            throw new ServiceException("存在非法的角色信息！请确认角色未删除");
        }
        roleRelationService.delByUser(userId);

        roleRelationService.saveUserRoles(userId, roleIds);
    }

    public List<Long> queryRoleIdsByUserId(Long userId) {
        return roleRelationService.queryRoleIdsByUserId(userId);
    }

    public List<RoleNameVo> queryRoleNamesByUserId(Long userId) {
        return roleRelationService.queryRoleNamesByUserId(userId);
    }
}
